//
//  XKPictureCollectionView.m
//  Selene
//
//  Created by 刘晓凯 on 2017/3/13.
//  Copyright © 2017年 hairun. All rights reserved.
//

#import "XKPictureCollectionView.h"
#import "XKPictureCollectionCell.h"

@interface XKPictureCollectionView ()<XKTapImageViewDelegate> {
    //小item宽高
    CGFloat _smallItemWH;
    //中item宽高
    CGFloat _mediumItemWH;
}
@end

@implementation XKPictureCollectionView

- (instancetype)initWithFrame:(CGRect)frame Images:(NSMutableArray *)images {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = sPictureItemSpacing;
    flowLayout.minimumInteritemSpacing = sPictureItemSpacing;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        self.imageArray = images;
        self.frame = [self correctFrame:frame Images:images];
        self.backgroundColor = [UIColor blueColor];
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[XKPictureCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([XKPictureCollectionCell class])];
    }
    return self;
}

//刷新数据
- (void)reloadPictureData {
    if (self.imageArray && self.imageArray.count > 0) {
        self.frame = [self correctFrame:self.frame Images:self.imageArray];
        [self reloadData];
    }
}

//根据images矫正frame
- (CGRect)correctFrame:(CGRect)frame Images:(NSMutableArray *)images {
    CGRect newFrame = CGRectZero;
    _smallItemWH = (frame.size.width - sPictureItemSpacing * 2.0) / 3.0;
    _mediumItemWH = (frame.size.width - sPictureItemSpacing) / 2.0;
    if (images.count == 1) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 3.0 + sPictureItemSpacing * 2.0;
        frame = newFrame;
    } else if (images.count == 2) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 3.0 + sPictureItemSpacing * 2.0;
        frame = newFrame;
    } else if (images.count == 3) {
        newFrame = frame;
        newFrame.size.height = _mediumItemWH + sPictureItemSpacing + _mediumItemWH / 3.0 * 4.0;
        frame = newFrame;
    } else if (images.count == 4) {
        newFrame = frame;
        newFrame.size.height = _mediumItemWH * 2.0 + sPictureItemSpacing;
        frame = newFrame;
    } else if (images.count == 5) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH + sPictureItemSpacing + _mediumItemWH;
        frame = newFrame;
    } else if (images.count == 6) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 3.0 + sPictureItemSpacing * 2.0;
        frame = newFrame;
    } else if (images.count == 7) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 2.0 + sPictureItemSpacing * 2.0 + _smallItemWH / 3.0 * 4.0;
        frame = newFrame;
    } else if (images.count == 8) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 2.0 + sPictureItemSpacing * 2.0 + _mediumItemWH;
        frame = newFrame;
    } else if (images.count >= 9) {
        newFrame = frame;
        newFrame.size.height = _smallItemWH * 3.0 + sPictureItemSpacing * 2.0;
        frame = newFrame;
    }
    return newFrame;
}

- (void)dealloc {
    self.pictureDelegate = nil;
}

#pragma mark -UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.imageArray.count == 6) {
        return 5;
    } else {
        return self.imageArray.count;
    }
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XKPictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XKPictureCollectionCell class]) forIndexPath:indexPath];
    cell.pictureOne.tapDelegate = self;
    
    if (self.imageArray.count == 6) {
        if (indexPath.item == 0) {
            cell.pictureOne.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
            cell.pictureTwo.hidden = YES;
            cell.pictureTwo.tapDelegate = nil;
            cell.pictureOne.identifier = [NSString stringWithFormat:@"%d",(int)indexPath.item];
            cell.pictureOne.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.item]];
            
        } else if (indexPath.item == 1) {
            cell.pictureOne.frame = CGRectMake(0, 0, cell.frame.size.width, (cell.frame.size.height-sPictureItemSpacing) / 2.0);
            cell.pictureTwo.hidden = NO;
            cell.pictureTwo.tapDelegate = self;
            cell.pictureOne.identifier = [NSString stringWithFormat:@"%d",(int)indexPath.item];
            cell.pictureTwo.identifier = [NSString stringWithFormat:@"%d",(int)(indexPath.item + 1)];
            cell.pictureOne.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.item]];
            cell.pictureTwo.image = [UIImage imageNamed:[self.imageArray objectAtIndex:(indexPath.item + 1)]];
            
        } else {
            cell.pictureOne.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
            cell.pictureTwo.hidden = YES;
            cell.pictureTwo.tapDelegate = nil;
            cell.pictureOne.identifier = [NSString stringWithFormat:@"%d",(int)(indexPath.item + 1)];
            cell.pictureOne.image = [UIImage imageNamed:[self.imageArray objectAtIndex:(indexPath.item + 1)]];
            
        }
    } else {
        cell.pictureOne.identifier = [NSString stringWithFormat:@"%d",(int)indexPath.item];
        cell.pictureOne.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.item]];
    }
    return cell;
}

#pragma mark -UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark -UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize viewSize = collectionView.frame.size;
    CGSize newSize = CGSizeZero;
    if (self.imageArray.count == 1) {
        newSize = viewSize;
    } else if (self.imageArray.count == 2) {
        newSize = CGSizeMake(_mediumItemWH, viewSize.height);
    } else if (self.imageArray.count == 3) {
        if (indexPath.item > 0) {
            newSize = CGSizeMake(_mediumItemWH, _mediumItemWH);
        } else {
            newSize = CGSizeMake(viewSize.width, _mediumItemWH / 3.0 * 4.0);
        }
    } else if (self.imageArray.count == 4) {
        newSize = CGSizeMake(_mediumItemWH, _mediumItemWH);
    } else if (self.imageArray.count == 5) {
        if (indexPath.item >= 2) {
            newSize = CGSizeMake(_smallItemWH, _smallItemWH);
        } else {
            newSize = CGSizeMake(_mediumItemWH, _mediumItemWH);
        }
    } else if (self.imageArray.count == 6) {
        if (indexPath.item >= 2) {
            newSize = CGSizeMake(_smallItemWH, _smallItemWH);
        } else if (indexPath.item == 1) {
            newSize = CGSizeMake(_smallItemWH, _smallItemWH * 2.0 + sPictureItemSpacing);
        } else {
            newSize = CGSizeMake((_smallItemWH * 2.0 + sPictureItemSpacing), (_smallItemWH * 2.0 + sPictureItemSpacing));
        }
    } else if (self.imageArray.count == 7) {
        if (indexPath.item >= 1) {
            newSize = CGSizeMake(_smallItemWH, _smallItemWH);
        } else {
            newSize = CGSizeMake(viewSize.width, _smallItemWH / 3.0 * 4.0);
        }
    } else if (self.imageArray.count == 8) {
        if (indexPath.item >= 2) {
            newSize = CGSizeMake(_smallItemWH, _smallItemWH);
        } else {
            newSize = CGSizeMake(_mediumItemWH, _mediumItemWH);
        }
    } else if (self.imageArray.count >= 9) {
        newSize = CGSizeMake(_smallItemWH, _smallItemWH);
    }
    CGSize itemSize = CGSizeMake(floor(newSize.width), floor(newSize.height));
//    NSLog(@"item size ----%@",NSStringFromCGSize(itemSize));
    return itemSize;
}

#pragma mark -XKTapImageViewDelegate

- (void)tapGestureWithObject:(XKTapImageView *)object {
    NSLog(@"点击了第%@个图片",object.identifier);
    if ([self.pictureDelegate respondsToSelector:@selector(selectedObject:dataArray:)]) {
        [self.pictureDelegate selectedObject:object.identifier dataArray:(NSArray *)self.imageArray];
    }
}

#pragma mark -懒加载
- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [[NSMutableArray alloc] init];
    }
    return _imageArray;
}

@end
